package com.rxnetwork.transferee;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rxnetwork.R;
import com.rxnetwork.transferee.tilibrary.view.image.PhotoView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaomeng on 2015/8/19.
 */
public class PhotoViewPagerActivity extends Activity {

    private ViewPager mPager;

    private TextView mIndexPageTv;

    private List<String> pathList;

    private int currPosition;

    /**
     * @param activity
     * @param pathList     图片集合
     * @param currPosition 当前选择项,默认0
     */
    public static void instancePhotoView(Activity activity, ArrayList<String> pathList, int currPosition) {
        Intent intent = new Intent(activity, PhotoViewPagerActivity.class);
        intent.putStringArrayListExtra("pathList", pathList);
        intent.putExtra("currPosition", currPosition);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_photo_view_pager);

//        StatusBarUtil.setStateBarTranslucent(this);

//        try {
//            //处理android 4.4以下会崩溃
//            StatusBarUtil.setColor(this, getResources().getColor(android.R.color.white), 127);
//        } catch (Exception e) {
//        }

        pathList = getIntent().getStringArrayListExtra("pathList");
        currPosition = getIntent().getIntExtra("currPosition", 0);
        if (pathList == null) return;

        mIndexPageTv = (TextView) findViewById(R.id.index_page_tv);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setTextViewIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        mPager.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));
        mPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return pathList == null ? 0 : pathList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                PhotoView photoView = new PhotoView(PhotoViewPagerActivity.this);
                photoView.enable();
                photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                Glide.with(PhotoViewPagerActivity.this)
                        .load(pathList.get(position))
                        .placeholder(R.mipmap.ic_empty_photo)
                        .into(photoView);
                container.addView(photoView);
                setClick(photoView);
                return photoView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });

        mPager.setCurrentItem(currPosition);
        setTextViewIndex(currPosition);
    }

    private void setTextViewIndex(int position) {
        mIndexPageTv.setText(position + 1 + "/" + mPager.getAdapter().getCount());
    }

    private void setClick(final PhotoView view) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(PhotoViewPagerActivity.this, PhotoBrowseFragment.class);
//                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
//        Intent intent = new Intent(PhotoViewPagerActivity.this, PhotoBrowseFragment.class);
//        intent.putExtra("selectIndex", currPosition);
//        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.in_right, R.anim.out_left);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.in_left, R.anim.out_right);
    }
}
